<template>
	<view class="container">
		<view class="info">
			<!-- 微信端 -->
			<!-- #ifdef MP-WEIXIN -->
			<view class="left">
				<view class="nickname" v-if="Object.values(LoginUser).length > 0">
					{{LoginUser.nickname}}
				</view>
				<view class="nickname" v-else>
					<open-data type="userNickName"></open-data>
				</view>
			</view>
			<view class="right">
				<view class="avatar">
					<u--image :src="LoginUser.avatar" width="80px" height="80px" shape="circle"
						v-if="Object.values(LoginUser).length > 0"></u--image>
					<open-data type="userAvatarUrl" v-else></open-data>
				</view>
			</view>
			<!-- #endif -->
			<!-- H5和APP -->
			<!--  #ifdef  H5 || APP-PLUS -->
			<view class="left">
				<view class="nickname" v-if="JSON.stringify(LoginUser) != '{}'">
					{{LoginUser.nickname}}
				</view>
				<view class="nickname" v-else>
					未登录
				</view>
				<view class="email" v-if="JSON.stringify(LoginUser) != '{}'">
					{{LoginUser.email}}
				</view>
			</view>
			<view class="right">
				<view class="avatar">
					<u-avatar :src="LoginUser.avatar" size="80" shape="circle"
						v-if="JSON.stringify(LoginUser) != '{}'"></u-avatar>
					<u-avatar src="/static/avatar.jpg" size="80" v-else></u-avatar>
				</view>
			</view>
			<!--  #endif -->
		</view>
		<view class="banner-warpper">
			<view class="banner">
				<view class="left" v-if="Object.values(LoginUser).length > 0">
					<view class="group">
						{{LoginUser.group_text}}
					</view>
					<view class="join">
						加入时间：{{LoginUser.createtime}}
					</view>
				</view>
				<view class="left" v-else>
					<view class="group">
						暂无角色组
					</view>
					<view class="join">
						加入时间：无
					</view>
				</view>
				<view class="right">
					<u-button type="default" :customStyle="FeedStyle" :plain="true" icon="chat" text="问题反馈"
						shape="circle" open-type="feedback" @click="ChangeTip"></u-button>
				</view>
			</view>
		</view>
		<view class="grid1" v-if="Object.values(LoginUser).length > 0">
			<u-grid :border="false" col="4">
				<u-grid-item v-for="(item,index) in operation" :key="index" @click="toPage(item.url)">
					<u-icon :customStyle="{paddingTop:20+'rpx'}" :name="item.name" color="#19BC9C" size="30"></u-icon>
					<text class="grid-text">{{item.title}}</text>
				</u-grid-item>
			</u-grid>
		</view>
		<view class="actions">
			<view class="contact">
				<u-button type="default" :customStyle="BtnStyle" :plain="true" icon="chat" open-type="contact"
					text="联系助手" @click="ChangeTip"></u-button>
			</view>
			<view class="logout">
				<!--  #ifdef  MP-WEIXIN -->
				<u-button type="error" text="注销登录" v-if="JSON.stringify(LoginUser) != '{}'"
					@click="show = true"></u-button>
				<u-button type="primary" text="微信一键登陆" v-else @click="login"></u-button>
				<!--  #endif -->
				<!-- H5和APP -->
				<!--  #ifdef  H5 || APP-PLUS -->
				<u-button type="error" text="注销登录" v-if="JSON.stringify(LoginUser) != '{}'"
					@click="show = true"></u-button>
				<u-button type="primary" text="帐号登陆" v-else @click="toPage('logins')"></u-button>
				<!--  #endif -->
			</view>
		</view>
		<!-- 消息提示 -->
		<u-toast ref="uToast"></u-toast>
		<!-- 确认框 -->
		<u-modal :show="show" :content='content' showCancelButton="true" @cancel="show = false"
			@confirm="logout"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				operation: [{
						name: 'coupon-fill',
						title: '修改资料',
						url: '/pages/admin/profile'
					},
					{
						name: 'checkmark-circle-fill',
						title: '微信解绑',
						url: '/pages/admin/wechat'
					},
					{
						name: 'man-add-fill',
						title: '领取记录',
						url: '/pages/business/receive/index'
					},
					{
						name: 'photo-fill',
						title: '回访记录',
						url: '/pages/business/visit/index'
					},
				],
				BtnStyle: {
					border: '1px solid #ebedf0'
				},
				FeedStyle: {
					width: '90px',
					height: '30px',
					marginRight: '10px'
				},
				src: 'https://cdn.uviewui.com/uview/album/1.jpg',
				LoginUser: {},
				show: false,
				content: '确认是否退出登陆？'
			}
		},
		onShow() {
			this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {}
		},
		methods: {
			ChangeTip() {
				// #ifdef H5 || APP-PLUS 
				this.$refs.uToast.show({
					type: 'default',
					message: "该功能只在微小程序生效",
				})
				// #endif
			},
			login() {
				uni.login({
					provider: 'weixin',
					success: async (res) => {
						console.log(res);
						const {
							code
						} = res
						let result = await this.$u.api.admin.login({
							code: code
						})

						if (result.code == 0) {
							this.$refs.uToast.show({
								type: 'error',
								message: result.msg,
								complete: () => {
									uni.navigateTo({
										url: '/pages/admin/bind'
									})
								}
							})
						} else {
							uni.setStorageSync('LoginUser', result.data);
							this.LoginUser = result.data
							this.$refs.uToast.show({
								type: 'success',
								message: result.msg,
							})
						}
					}
				})
			},
			logout() {
				this.show = false
				uni.removeStorageSync('LoginUser');
				this.LoginUser = {}
			},
			toPage(url) {
				uni.navigateTo({
					url: url
				})
			},
		},
	}
</script>

<style>
	page {
		background-color: #fff;
	}

	.container {
		margin: 0;
	}

	.info {
		display: flex;
		padding: 40rpx;
		background-color: #f5f5f5;
		justify-content: space-between;
	}

	.info .left {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	/* #ifdef MP-WEIXIN  */
	.info .left {
		display: flex;
	}

	/* #endif    */
	.info .right .xcx {
		width: 200rpx;
		height: 200rpx;
		border-radius: 100%;
		overflow: hidden;
	}

	.info .left .nickname {
		font-size: 26px;
		font-weight: bold;
	}

	.info .left .email {
		font-size: 12px;
		color: gray;
	}

	.banner-warpper {
		background-color: #f5f5f5;
	}

	.banner {
		width: auto;
		margin: 0 40rpx;
		border-top-right-radius: 10px;
		border-top-left-radius: 10px;
		height: 60px;
		background: rgb(34, 52, 70);
		background: linear-gradient(133deg, rgba(34, 52, 70, 1) 35%, rgba(44, 62, 80, 1) 100%);
		box-shadow: 0 10px 10px var(--color-dark);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.banner .left {
		flex: 3;
		display: flex;
		flex-direction: column;
		justify-content: center;
		text-align: center;
	}

	.banner .right {
		flex: 2;
		display: flex;
		justify-content: center;
	}

	.banner .left .group {
		color: #fff;
		font-size: 16px;
		font-weight: bold;
	}

	.banner .left .join {
		color: #aaa;
		font-size: 10px;
		padding-top: 4px;
	}

	.grid1 {
		padding: 20px 40rpx;
		background-color: #fff;
	}

	.grid1 .van-grid-item {
		color: var(--color-primary);
	}

	.grid2 {
		padding: 0 40rpx;
		background-color: #fff;
	}

	.grid2 .title {
		font-size: 16px;
		font-weight: bold;
		padding: 0 10rpx 10px;
	}

	.actions {
		display: flex;
		padding: 20px 40rpx;
	}

	.contact {
		flex: 1;
		padding-right: 4px;
	}

	.logout {
		flex: 1;
		padding-left: 4px;
	}

	.btn-vertical {
		background-color: rgb(0, 0, 0, 0) !important;
		border: 0 !important;
		width: 100%;
		height: 100% !important;
		padding: 15px !important;
	}

	.btn-vertical.van-button--active:before {
		opacity: 1;
		background-color: rgb(0, 0, 0, 0) !important;
	}

	.btn-vertical__icon {
		font-size: 26px;
		align-items: center;
		justify-content: center;
	}

	.btn-vertical__text {
		margin-top: 6px;
		word-wrap: break-word;
		color: var(--grid-item-text-color, #646566);
		font-size: var(--grid-item-text-font-size, 12px);
	}
</style>